package com.zhihuishu.micro.course.openapi.app.impl.teacher;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.google.common.collect.Lists;
import com.zhihuishu.micro.course.log.LoggerCollectionEnum;
import com.zhihuishu.micro.course.log.LoggerTemplateUtil;
import com.zhihuishu.micro.course.model.TipInfo;
import com.zhihuishu.micro.course.model.UserViewLog;
import com.zhihuishu.micro.course.openapi.app.dto.TipInfoOpenDto;
import com.zhihuishu.micro.course.openapi.app.dto.UserViewLogOpenDto;
import com.zhihuishu.micro.course.openapi.app.teacher.UserViewLogOpenService;
import com.zhihuishu.micro.course.service.UserViewLogService;
import com.zhihuishu.remote.RemoteResult;

@Service("userViewLogOpenServiceForTeacherApp")
public class UserViewLogOpenServiceImpl implements UserViewLogOpenService {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private UserViewLogService userViewLogService;

    @Override
    public RemoteResult<List<TipInfoOpenDto>> queryUserUpdatesDetial(UserViewLogOpenDto userViewLogOpenDto) {
        RemoteResult<List<TipInfoOpenDto>> remoteResult = new RemoteResult<List<TipInfoOpenDto>>();
        try {
            Assert.notNull(userViewLogOpenDto, "查询对象不能为空");
            Assert.isTrue(userViewLogOpenDto.getCourseId() > 0, "课程id必须大于0");
            Assert.isTrue(userViewLogOpenDto.getUserId() > 0, "用户参数不合法");
            Assert.isTrue(userViewLogOpenDto.getModule() > 0, "模块参数不合法");
            Assert.isTrue(userViewLogOpenDto.getModule() < 7, "模块参数不合法");
            if (logger.isDebugEnabled()) {
                logger.debug("用户查询Tips信息传入的参数为{}", userViewLogOpenDto);
            }
            UserViewLog userViewLog = new UserViewLog();
            userViewLog.setCourseId(userViewLogOpenDto.getCourseId());// 课程
            userViewLog.setUserId(userViewLogOpenDto.getUserId());// 用户
            userViewLog.setModule(userViewLogOpenDto.getModule());// 模块
            List<TipInfo> tipInfos = userViewLogService.queryUserUpdatesDetial(userViewLog);
            List<TipInfoOpenDto> infoOpenDtos = Lists.newArrayList();
            TipInfoOpenDto tipInfoOpenDto = null;
            for (TipInfo tipInfo : tipInfos) {
                tipInfoOpenDto=new TipInfoOpenDto();
                tipInfoOpenDto.setCourseId(tipInfo.getCourseId());
                tipInfoOpenDto.setModule(tipInfo.getModule());
                tipInfoOpenDto.setChangePointEnumId(tipInfo.getChangePoint());
                tipInfoOpenDto.setPointId(tipInfo.getPointId());
                tipInfoOpenDto.setUserId(tipInfo.getUserId());
                infoOpenDtos.add(tipInfoOpenDto);
            }
            remoteResult.setResult(infoOpenDtos);
        } catch (Exception e) {
            remoteResult.setExceptionStack(e);
            remoteResult.setSuccess(false);
            logger.error("用户查询Tip信息出错,参数为{}", userViewLogOpenDto, e);
        }
        return remoteResult;

    }

    @Override
    public RemoteResult<Void> saveUserViewLog(UserViewLogOpenDto userViewLogOpenDto) {
        RemoteResult<Void> remoteResult = new RemoteResult<Void>();
        try {
            Assert.notNull(userViewLogOpenDto, "查询对象不能为空");
            Assert.notNull(userViewLogOpenDto.getChangePointEnum(), "模块参数不合法");
            Assert.isTrue(userViewLogOpenDto.getUserId() > 0, "用户参数不合法");
            Assert.isTrue(userViewLogOpenDto.getCourseId() > 0, "课程id必须大于0");
            Assert.isTrue(userViewLogOpenDto.getModule() > 0, "模块参数不合法");
            Assert.isTrue(userViewLogOpenDto.getModule() < 7, "模块参数不合法");
            if (logger.isDebugEnabled()) {
                logger.debug("用户查看日志记录对象传递的参数为{}", userViewLogOpenDto);
            }
            if (userViewLogOpenDto.getModule() == 3) {// 点击进去
                Assert.isTrue(userViewLogOpenDto.getChangePointEnum().getCode() > 0, "模块参数不合法");
                Assert.isTrue(userViewLogOpenDto.getViewPointId() > 0, "观看点id不合法");
            } else {// 进入的是 1 2 4 5 6步的页面
                Assert.isTrue(userViewLogOpenDto.getChangePointEnum().getCode() == 0, "模块参数不合法");
                Assert.isTrue(userViewLogOpenDto.getViewPointId() == 0, "观看点id不合法");
            }
            UserViewLog userViewLog = new UserViewLog();
            userViewLog.setCourseId(userViewLogOpenDto.getCourseId());
            userViewLog.setUserId(userViewLogOpenDto.getUserId());
            userViewLog.setModule(userViewLogOpenDto.getModule());
            userViewLog.setViewPoint(userViewLogOpenDto.getChangePointEnum().getCode());
            userViewLog.setViewPointId(userViewLogOpenDto.getViewPointId());
            userViewLogService.saveUserViewLog(userViewLog);
            LoggerTemplateUtil.saveLogger(String.format("用户查看日志记录:%s", userViewLog.toString()),
                    userViewLog.getUserId(), LoggerCollectionEnum.userViewLog, userViewLog.getCourseId());

        } catch (Exception e) {
            remoteResult.setExceptionStack(e);
            remoteResult.setSuccess(false);
            logger.error("保存用户观看日志出错,参数为:{}", userViewLogOpenDto, e);
        }
        return remoteResult;
    }

    @Override
    public RemoteResult<Map<Integer, Integer>> queryUpdateCountForMainPage(
            UserViewLogOpenDto userViewLogOpenDto) {
        RemoteResult<Map<Integer, Integer>> remoteResult=new RemoteResult<Map<Integer,Integer>>();
        try {
            Assert.notNull(userViewLogOpenDto,"参数不合法");
            Assert.isTrue(userViewLogOpenDto.getCourseId()>0, "参数不合法");
            Assert.isTrue(userViewLogOpenDto.getUserId()>0, "参数不合法");
            Map<Integer, Integer> map=userViewLogService.queryUpdateCountForMainPage(userViewLogOpenDto.getCourseId(),userViewLogOpenDto.getUserId());
            remoteResult.setResult(map);
        } catch (Exception e) {
            remoteResult.setExceptionStack(e);
            remoteResult.setSuccess(false);
            logger.error("查询主页更新信息出错,参数为{}",userViewLogOpenDto,e);
        }
        return remoteResult;
    }

}
